package com.tankgame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: FENGYiMing
 * @Date: 2024/03/29/11:29
 * @Description: 坦克大战的绘图区域
 */
public class MyPanel extends JPanel implements KeyListener, Runnable {
    /**
     * 自己的坦克
     */
    Hero hero = null;
    /**
     * 敌人坦克们
     */
    Vector<EnemyTank> enemyTanks = new Vector<>();
    /**
     * 用于存放爆炸图片
     */
    static Vector<Bomb> bombs = new Vector<>();
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;


    public MyPanel() {
        hero = new Hero(500, 500, 0);
        hero.setSpeed(10);
        /*
          初始化敌人坦克
         */
        int enemyTankSize = 3;
        for (int i = 0; i < enemyTankSize; i++) {
            EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 0, 2);
            //给坦克添加子弹
            Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirection());
            enemyTank.shots.add(shot);
            new Thread(shot).start();
            enemyTanks.add(enemyTank);
            enemyTank.setEnemyTankList(enemyTanks);
            new Thread(enemyTank).start();

        }

        /*
          初始化爆炸图片
         */
        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb1.png"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb2.png"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb3.png"));
    }

    public void showInfo(Graphics g) {
        g.setColor(Color.BLACK);
        Font font = new Font("宋体", Font.BOLD, 20);
        g.setFont(font);
        g.drawString("您累计击毁坦克数：", 930, 30);
        drawTank(930, 60, g, 0, 1);
        g.setColor(Color.BLACK);
        g.drawString(Recoder.getAllEnemyTankNum() + "", 1000, 100);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        showInfo(g);
        g.fillRect(0, 0, 900, 650);
        if (hero != null && hero.isLive()) {
            drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), 0);
        }

        if (hero != null) {
            drawMultipleBullets(g, hero.shots);
        }

        //敌方坦克
        for (EnemyTank item : enemyTanks) {
            //如何敌方坦克还活着，重绘坦克，实现坦克死亡效果
            if (item.isLive()) {
                drawTank(item.getX(), item.getY(), g, item.getDirection(), 1);
                drawMultipleBullets(g, item.shots);
            }
        }
        //如果有bombs 集合中有对象,就画出图片
        for (int i = 0; i < bombs.size(); i++) {
            //加上这个睡眠可以使第一次爆炸效果出现
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Bomb bomb = bombs.get(i);
            //根据bomb的生命值画出图片
            if (bomb.getLive() > 6) {
                g.drawImage(image1, bomb.getX(), bomb.getY(), 60, 60, this);
            } else if (bomb.getLive() > 3) {
                g.drawImage(image2, bomb.getX(), bomb.getY(), 60, 60, this);
            } else {
                g.drawImage(image3, bomb.getX(), bomb.getY(), 60, 60, this);
            }
            bomb.lifeDown();
            if (bomb.getLive() == 0) {
                bombs.remove(bomb);
            }
        }
    }

    /**
     * 绘制多个子弹
     *
     * @param g     画笔
     * @param shots 子弹群
     */
    private void drawMultipleBullets(Graphics g, Vector<Shot> shots) {
        for (int i = 0; i < shots.size(); i++) {
            Shot shot = shots.get(i);
            if (shot.isLive()) {
                drawBullet(shot.getX(), shot.getY(), g);
            } else {
                shots.remove(shot);
            }
        }
    }

    /**
     * 击中坦克
     *
     * @param shot 子弹
     * @param tank 坦克
     */
    public void hitTank(Shot shot, Tank tank) {
        switch (tank.getDirection()) {
            case 0:
            case 2:
                hitDirect(shot, tank, 60, 40);
                break;
            case 1:
            case 3:
                hitDirect(shot, tank, 40, 60);
                break;
            default:
                break;
        }
    }

    private void hitDirect(Shot shot, Tank tank, int n1, int n2) {
        if (shot.getX() > tank.getX() && shot.getX() < tank.getX() + n1 && shot.getY() > tank.getY() && shot.getY() < tank.getY() + n2) {
            shot.setLive(false);
            tank.setLive(false);
            Bomb bomb = new Bomb(tank.getX(), tank.getY());
            bombs.add(bomb);
            if (tank instanceof EnemyTank) {
                enemyTanks.remove(tank);
                Recoder.addAllEnemyTankNum();
            }
            //如何销毁hero坦克？？？？
            if (tank instanceof Hero) {

            }
        }
    }

    /**
     * 绘制坦克
     *
     * @param x         x轴坐标
     * @param y         y轴坐标
     * @param g         画笔
     * @param direction 方向
     * @param type      类型
     */
    public void drawTank(int x, int y, Graphics g, int direction, int type) {
        switch (type) {
            case 0:
                //我们的坦克
                g.setColor(Color.cyan);
                break;
            case 1:
                //敌人的坦克
                g.setColor(Color.YELLOW);
                break;
            default:
                System.out.println("不是坦克");
        }
        //0：向上  1：向右  2：向下  3：向左
        switch (direction) {
            case 0:
                //画出坦克的左边轮子
                g.fill3DRect(x, y, 10, 60, false);
                //画出坦克的右边轮子
                g.fill3DRect(x + 30, y, 10, 60, false);
                //画出坦克的盖子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                //画出圆形盖子
                g.fillOval(x + 10, y + 20, 20, 20);
                //画出炮筒
                g.drawLine(x + 20, y + 30, x + 20, y);
                break;
            case 1:
                //画出坦克的左边轮子
                g.fill3DRect(x, y, 60, 10, false);
                //画出坦克的右边轮子
                g.fill3DRect(x, y + 30, 60, 10, false);
                //画出坦克的盖子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                //画出圆形盖子
                g.fillOval(x + 20, y + 10, 20, 20);
                //画出炮筒
                g.drawLine(x + 30, y + 20, x + 60, y + 20);
                break;
            case 2:
                //画出坦克的左边轮子
                g.fill3DRect(x, y, 10, 60, false);
                //画出坦克的右边轮子
                g.fill3DRect(x + 30, y, 10, 60, false);
                //画出坦克的盖子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                //画出圆形盖子
                g.fillOval(x + 10, y + 20, 20, 20);
                //画出炮筒
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                break;
            case 3:
                //画出坦克的左边轮子
                g.fill3DRect(x, y, 60, 10, false);
                //画出坦克的右边轮子
                g.fill3DRect(x, y + 30, 60, 10, false);
                //画出坦克的盖子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                //画出圆形盖子
                g.fillOval(x + 20, y + 10, 20, 20);
                //画出炮筒
                g.drawLine(x + 30, y + 20, x, y + 20);
                break;
            default:
                System.out.println("没有方向");
        }
    }

    /**
     * 绘制子弹
     *
     * @param x x轴坐标
     * @param y y轴坐标
     * @param g 画笔
     */
    public void drawBullet(int x, int y, Graphics g) {
        g.fill3DRect(x, y, 2, 2, false);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_W) {
            hero.setDirection(0);
            hero.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            hero.setDirection(1);
            hero.moveRight();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.setDirection(2);
            hero.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.setDirection(3);
            hero.moveLeft();
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            hero.shotTank();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            /* 当发射多课子弹后，每颗子弹都会杀死敌人 */
            for (int j = 0; j < hero.shots.size(); j++) {
                Shot shot = hero.shots.get(j);
                //判断是否击中坦克
                if (shot.isLive()) {
                    for (int i = 0; i < enemyTanks.size(); i++) {
                        EnemyTank item = enemyTanks.get(i);
                        hitTank(shot, item);
                    }
                }
            }
            //敌人打过来的子弹也可以杀死自己
            for (int j = 0; j < enemyTanks.size(); j++) {
                EnemyTank enemyTank = enemyTanks.get(j);
                //判断是否击中坦克
                if (enemyTank.isLive()) {
                    for (int i = 0; i < enemyTank.shots.size(); i++) {
                        Shot shot = enemyTank.shots.get(i);
                        hitTank(shot, hero);
                    }
                }
            }
            this.repaint();
        }
    }
}
